// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/dqls/index_hint.h>
#include <cantor/transform/expr.h>


namespace cantor::transform {
    void TransIndexHint::resolve_ast(const cantor::parser::IndexHint *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        index_hint.set_hint_type(node->hint_type);
        index_hint.set_hint_scope(node->hint_scope);
        auto name_list = index_hint.mutable_index_name_list();
        name_list->Reserve(node->index_name_list.size());
        for (int i = 0; i < node->index_name_list.size(); ++i) {
            name_list->Add()->assign(node->index_name_list[i].to_string());
        }

        set_proto_location(node, index_hint.mutable_location());
        set_proto_status(index_hint.mutable_status());
    }

    const turbo::Status &TransIndexHint::check() {
        if (!status.ok()) {
            return status;
        }
        if (!index_hint.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        return status;
    }

    turbo::Status TransIndexHint::resolve_to(turbo::Nonnull<const cantor::parser::IndexHint *> arg,
                                        turbo::Nonnull<pb::ast::IndexHint *> out) {
        TransIndexHint trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.index_hint);
        return status;
    }
} // namespace cantor::transform
